home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / splash / spltest.cpp < prev    next >
Text File  |  1993-01-14  |  13KB  |  372 lines

  1. #ifdef    TEST
  2.  
  3. #include <iostream.h>
  4. /*
  5.  * V1.8
  6.  */
  7.  
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <stdio.h>
  11.  
  12. #ifdef    __TURBOC__
  13. #pragma hdrstop
  14. #endif
  15.  
  16. #include "splash.h"
  17.  
  18. int main()
  19. {
  20. SPList<int> il, il1, il2, il3;
  21. SPStringList x, y, z;
  22. int n;
  23.  
  24.     if(x) cout << "x is not empty" << endl;
  25.     else cout << "x is empty" << endl;
  26.  
  27.     if(x.isempty()) cout << "x.isempty() is true" << endl;
  28.     else cout << "x.isempty() is false" << endl;
  29.  
  30.     n= x.split("a b c d e f");
  31.  
  32.     if(x) cout << "x is not empty" << endl;
  33.     else cout << "x is empty" << endl;
  34.  
  35.     if(x.isempty()) cout << "x.isempty() is true" << endl;
  36.     else cout << "x.isempty() is false" << endl;
  37.  
  38.     cout << "x.split(a b c d e f)= " << n << ": " << x << endl;
  39.     cout << "x[0] = " << x[0] << endl;
  40.     z= x; z[0]= "x";
  41.     cout << "z= x; z[0]=\"x\" " << "z: " << z << endl;
  42.  
  43.     SPString ss("1.2.3.4.5.6.7.8.9.0");
  44.     y= ss.split("\\.");
  45.     cout << "ss= " << ss << ", y= ss.split(\"\\.\"), y=" << endl << y << endl;
  46.     cout << "y.join(\" \")" << y.join(" ") << endl;       
  47.     {
  48.     SPString xx= "a b c\nd e\tf   g";
  49.         cout << xx << endl << "xx.split()= " << xx.split() << endl;
  50.     xx= "a b c d e f g";
  51.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  52.     xx= "  a b c d e f g hi  ";
  53.     cout << xx << endl << "xx.split(\"\")= " << xx.split("") << endl;
  54.     xx= "a,b,c,d,,e,f,g,,,,";
  55.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  56.     xx= "a,b,c,d,,e,f,g,,";
  57.     cout << xx << endl << "xx.split(\",\", 5)= " << xx.split(",", 5) << endl;
  58.     xx= " a b c d e f g  ";
  59.     cout << xx << endl << "xx.split(\" \")= " << xx.split(" ") << endl;
  60.     xx= "a b c d,e,f g";
  61.     cout << xx << endl << "xx.split(\"([ ,])+\")= " << xx.split("([ ,])+") << endl;
  62.     xx= ",,,,";
  63.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  64.     xx= "";
  65.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  66.      xx= "   a b c\td    e\nf  g   ";
  67.     cout << xx << endl << "xx.split(\"' '\")= " << xx.split("' '") << endl;
  68.    }
  69.  
  70.     cout << "x = " << x << endl;
  71.  
  72.     cout << "x.pop() : " << x.pop() << ", " ;
  73.     cout << x.pop() << endl << "x= " << x << endl;;
  74.     cout << "x.shift() : " << x.shift() << ", ";
  75.     cout << x.shift() << endl<< "x= " << x << endl;
  76.     
  77.     x.unshift(y);
  78.     cout << "x.unshift(y): " << x << endl;
  79.     
  80.     if(il) cout << "il is not empty" << endl;
  81.     else cout << "il is empty" << endl;
  82.     
  83.     il.push(1); il.push(2); il.push(3); il.push(4);
  84.  
  85.     if(il) cout << "il is not empty" << endl;
  86.     else cout << "il is empty" << endl;
  87.     
  88.     cout << "il(1, 2, 3, 4) : " << il << endl;
  89.     il3= il; il3[0]= 9999;
  90.     cout << "il3= il; il3[0]= 9999; il3 = " << il3 << endl << "il= " << il << endl;
  91.  
  92.     il1= il.reverse();
  93.     cout << "il.reverse: " << il1 << endl;
  94.  
  95.     cout << "il1.sort(): " << il1.sort() << endl;
  96.  
  97.     y.reset();
  98.     y.push("one"); y.push("two"); y.push("three"); y.push("four");
  99.     cout << "y = " << endl << y;
  100.     cout << "y.reverse() " << y.reverse() << endl;
  101.     cout << "y.sort() " << y.sort() << endl;
  102.     cout << "y.sort().reverse() " << y.sort().reverse() << endl;
  103.         
  104.     il2.push(3); il2.push(4);
  105.     cout << "il2.push(3, 4) : " << il2 << endl;
  106.  
  107.     il.push(il2);    
  108.     cout << "il.push(il2) : " << il << endl;
  109.     
  110.     cout << "il.pop() : " << il.pop() << ", ";
  111.     cout << il.pop() << endl;
  112.     
  113.     il.unshift(il2);    
  114.     cout << "il.unshift(il2) : " << il << endl;
  115.     
  116.     cout << "il.shift() : " << il.shift() << ", ";
  117.     cout << il.shift() << endl;
  118.  
  119.     il.reset();
  120.     il.push(1); il.push(2);
  121.     if(il.shift() != 1) cout << "FIFO1 error" << endl;
  122.     if(il.shift() != 2) cout << "FIFO2 error" << endl;
  123.  
  124.     for(int i=0;i<100;i++) il.push(i);
  125.     i= 0;
  126.     while(il){
  127.     if(il.shift() != i)  cout << "FIFO3 error" << endl;
  128.     i++;
  129.     }
  130.     if(i != 100) cout << "FIFO over/under run" << endl;
  131.     
  132.     cout << "testing splice:" << endl;
  133.     x.reset();
  134.     x.split("a b c d e f g h i");
  135.     cout << "x = " << x << endl;
  136.     z= x.splice(2, 3);
  137.     cout << "z= x.splice(2, 3): z= " << z << endl << "x = " << x << endl;
  138.     cout << "x.splice(2, 0, z): " << x.splice(2, 0, z);
  139.     cout << "x= " << x << endl;
  140.     cout << "z.splice(1, 1, x): " << z.splice(1, 1, x);
  141.     cout << "z= " << z << endl;
  142.     cout << "x= " << x << endl;
  143.     cout << "z.splice(20, 1, x): " << z.splice(20, 1, x);
  144.     cout << "z= " << z << endl;
  145.     
  146.  // test auto expand
  147.     SPList<int> ile;
  148.     ile[3]= 3;
  149.     cout << ile.scalar() << ", " << ile[3] << endl;
  150.     ile[100]= 1234;
  151.     ile[0]= 5678;
  152.     cout << ile.scalar() << ", " << ile[0] << ", " << ile[100] << endl;
  153.     SPList<int> ile2;
  154.     for(i=0;i<=100;i++) ile2[i]= i;
  155.     for(i=200;i>100;i--) ile2[i]= i;
  156.     for(i=0;i<=200;i++) if(ile2[i] != i) cout << "error at index " << i << endl;
  157.     cout << "Index check done" << endl;
  158.     cout << ile2.scalar() << ", " << ile2[0] << ", " << ile2[200] << endl;
  159.  
  160. // test Regexp stuff
  161.     cout << endl << "testing regexp stuff:" << endl;
  162.     x.reset();
  163.     cout << "x.m(\".*X((...)...(...))\", \"12345Xabcxyzdef\") returns " <<
  164.      x.m(".*X((...)...(...))", "12345Xabcxyzdef") << endl;
  165.     cout << "subs matched = " << x << endl;
  166.  
  167.     Regexp rexp("abc");
  168.     SPString rst("12345Xabcxyzdef");
  169.     cout << "rst.m(rexp) returns " << rst.m(rexp) << endl;
  170.     
  171.     cout << endl << "testing grep:" << endl;
  172.     x.reset();
  173.     x.push("abcd"); x.push("a2345"); x.push("X2345"); x.push("Xaaaaa"); x.push("aaaaa");
  174.     
  175.     y= x.grep("^a.*");
  176.     cout << "x: " << endl << x << endl << "grep(^a.*)" << endl;
  177.     cout << "Expect 3 matches:" << endl << y << endl;
  178.     {
  179.     SPString s1("abcdef");
  180.         cout << "s1= " << s1 << ", s1.m(\"^cde\") : " << s1.m("^cde") << endl;
  181.     cout << "s1= " << s1 << ", s1.m(\"^..cde\") : " << s1.m("^..cde") << endl;
  182.     }
  183.     {
  184.     SPStringList sl;
  185.     SPString str= "ab cd ef";
  186.     sl = m("(..) (..)", str);
  187.     cout << "sl = m(\"(..) (..)\", \"ab cd ef\"); sl = " << endl <<
  188.          sl << endl;
  189.     }
  190.  
  191.     {
  192.     Regexp ncr("abc", Regexp::nocase);
  193.     Regexp cr("abc");
  194.     SPString s= "ABC";
  195.     cout << "s= " << s << ": s.m(ncr)= " << s.m(ncr) << endl;
  196.     cout << "s= " << s << ": s.m(cr)= " << s.m(cr) << endl;
  197.     cout << "s.m(\"abc\", \"i\")= " << s.m("abc", "i") << endl;
  198.     cout << "s.m(\"abc\")= " << s.m("abc") << endl;
  199.     }
  200.     
  201. // Test strings
  202.     cout << "test string stuff:" << endl;
  203.  
  204.     SPString s1("string1"), s2, s3;
  205.     const char *s= s1;
  206.  
  207.     cout << "Empty string: " << s2 << " length= " << s2.length()
  208.      << ",  strlen(s2) = " << strlen(s2) << endl;
  209.     
  210.     cout << "s1:" << s1 << endl;
  211.     cout << "s[0]= " << s[0] << ", s[5]= " << s[5] << endl;
  212. //    s[2]= 'X';
  213. //    cout << "s[2]= 'X', s= " << s << endl;
  214. //    s[2]= 'r';
  215.     
  216.     cout << "const char *s= s1: s= " << s << endl;
  217.     s2= s1;
  218.     cout << "s2=s1,  s2:" << s2 << endl;
  219.     s1.chop();
  220.     cout << "s1.chop()" << s1 << endl;
  221.     s3= s;
  222.     cout << "s3= s: s3 = " << s3 << endl;
  223.     cout << "index(\"ri\") in " << s1 << ": " << s1.index("ri") << endl;
  224.     s3= "1";
  225.     cout << "index(" << s3 << ") in " << s1 << ": " << s1.index(s3) << endl;
  226.     s3= "abcabcabc";
  227.     cout << "rindex(abc) in" << s3 << ": " << s3.rindex("abc") << endl;
  228.     cout << "rindex(abc, 5) in" << s3 << ": " << s3.rindex("abc", 5) << endl;
  229.  
  230. // test substrings
  231.     cout << "substr(5, 3) in " << s3 << ": " << s3.substr(5, 3) << endl;
  232.     s3.substr(5, 3)= "XXX";
  233.     cout << "s3.substr(5, 3) = \"XXX\"" << s3 << endl;
  234.     s3.substr(5, 3)= s1;
  235.     cout << "s3.substr(5, 3) = s1" << s3 << endl;
  236.     s3.substr(5, 3)= s1.substr(1, 3);
  237.     cout << "s3.substr(5, 3) = s1.substr(1, 3)" << s3 << endl;
  238.     s3.substr(0, 6)= s1.substr(0, 3);
  239.     cout << "s3.substr(0, 6) = s1.substr(0, 3)" << s3 << endl;
  240.     s3.substr(-3, 2)= s1.substr(0, 2);
  241.     cout << "s3.substr(-3, 2) = s1.substr(0, 2)" << s3 << endl;
  242.  
  243. // test overlapping substrings
  244.     s1= "1234567890";
  245.     cout << "s1 = " << s1 << endl;
  246.     s1.substr(0, 10)= s1.substr(1, 9);
  247.     cout << "s1.substr(0, 10)= s1.